package com.ciwong.epaper.modules.dictation.adpter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.adapter.BaseCommonAdapter;
import com.ciwong.epaper.modules.dictation.bean.DictationAnswerInfo;

import java.util.List;

/**
 * Created by keepon on 2018/7/13.
 */
public class DictationRedoAdapter extends BaseCommonAdapter<DictationAnswerInfo> {

	private Context mContext;

	public DictationRedoAdapter(Context context, List<DictationAnswerInfo> dataList) {
		super(dataList);
		mContext = context;
	}
	public DictationRedoAdapter(List<DictationAnswerInfo> dataList) {
		super(dataList);
	}

	@Override
	public int getItemResId(int position) {
		return R.layout.item_dictation_redo;
	}

	@Override
	public DictationRedoAdapter.ViewHolder createViewHolder(View convertView, int position) {
		DictationRedoAdapter.ViewHolder viewHolder = new DictationRedoAdapter.ViewHolder();
		viewHolder.mTvWord = (TextView) convertView.findViewById(R.id.tv_word);
		viewHolder.mIvIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
		viewHolder.mLine =   convertView.findViewById(R.id.line);
		viewHolder.mRelativeLayout =   convertView.findViewById(R.id.rl_container);
		return viewHolder;
	}

	@Override
	public void showData(final DictationAnswerInfo listenWordAnswerInfo, Object holder, final int position) {
		if (holder instanceof DictationRedoAdapter.ViewHolder) {
			DictationRedoAdapter.ViewHolder viewHolder = (ViewHolder) holder;
			int curentIndex = position + 1;
			String wordAnswer = listenWordAnswerInfo.getWordAnswer();
			viewHolder.mTvWord.setText(curentIndex + "." +(!TextUtils.isEmpty(wordAnswer)?wordAnswer:"") );
			if (listenWordAnswerInfo != null) {
				if (listenWordAnswerInfo.isRight()) {
					viewHolder.mIvIcon.setImageResource(R.mipmap.icon_answer_right);
					if(mContext!=null){
						viewHolder.mTvWord.setTextColor(mContext.getResources().getColor(R.color.color_light_green));
					}
				} else {
					if(mContext!=null){
						viewHolder.mTvWord.setTextColor(mContext.getResources().getColor(R.color.light_red));
					}
					viewHolder.mIvIcon.setImageResource(R.mipmap.icon_answer_wrong);

				}
				viewHolder.mRelativeLayout.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(mOnRedoWordListener!=null&&!listenWordAnswerInfo.isRight()){
							mOnRedoWordListener.redoWord(position);
						}
					}
				});
			}
			if(position==getCount()-1){
				viewHolder.mLine.setVisibility(View.INVISIBLE);
			}else{
				viewHolder.mLine.setVisibility(View.VISIBLE);
			}
		}
	}

	public void setOnRedoWordListener(OnRedoWordListener reReadWordListener) {
		this.mOnRedoWordListener = reReadWordListener;
	}
	OnRedoWordListener mOnRedoWordListener;
	public interface OnRedoWordListener {
		 void redoWord(int position);
	}


	public static class ViewHolder {
		RelativeLayout mRelativeLayout;
		TextView       mTvWord;
		ImageView      mIvIcon;
		ImageView      mLine;
	}
}
